package com.imu.purse.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.imu.common.annotation.IgnoreAuth;
import com.imu.common.utils.R;
import com.imu.purse.modules.base.entity.DictInfo;
import com.imu.purse.modules.base.service.DictInfoService;
import com.xiaoleilu.hutool.collection.CollUtil;
import com.xiaoleilu.hutool.util.RandomUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api")
@Api(tags = "字典接口")
public class ApiDictController 
{
    @Autowired
    private DictInfoService service;
    
    @IgnoreAuth
    @GetMapping("/dict/{key}")
    @ApiOperation("获取字典信息")
    private R getDict(@PathVariable("key") String key)
    {
        return R.ok(1).put("data", service.findCahceByCode(key));
    }
    
    @IgnoreAuth
    @GetMapping("/dict/random/{key}")
    @ApiOperation("获取字典信息")
    private R getRandomDict(@PathVariable("key") String key)
    {
    	List<DictInfo> dtos = service.findCahceByCode(key);
    	if(CollUtil.isNotEmpty(dtos))
    	{
    	    if(dtos.size()>1)
    		    return R.ok(1).put("data", dtos.get(RandomUtil.randomInt(0, dtos.size()-1)));
    	    else return R.ok(1).put("data", dtos.get(0));
    	}
    	
        return R.ok(1);
    }
}
